#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tmf_meta_instances.py 
@version:
@time: 2022/04/29 
"""
from pprint import pprint

from TapClientCore.T02_tm_api.v2_api.v2_tm import v2tm as TM


class TMFInstances:
    @staticmethod
    def _get_metadataInstances_by_filter(filter):
        return TM.get_metadataInstances_by_filter(filter).get('data')

    @staticmethod
    def _delete_metadataInstances_by_id(meta_id):
        return TM.delete_metadataInstances_by_id(meta_id).get('data')

    @staticmethod
    def _get_metadataInstances_find_table_by_id(meta_id):
        return TM.get_metadataInstances_find_table_by_id(meta_id).get('data')

    @staticmethod
    def _patch_metadataInstances_by_id(meta_id, payload):
        return TM.patch_metadataInstances_by_id(meta_id, payload).get('data')

    @staticmethod
    def _get_metadataInstances_search(meta_type, keyword, last_id=None):
        return TM.get_metadataInstances_search(meta_type, keyword, last_id).get('data')

    @staticmethod
    def _get_metadataInstances_table_by_connection_id(connection_id):
        return TM.get_metadataInstances_table_by_connection_id(connection_id).get('data')

    # 通过数据源id获取数据源下的模型
    def get_metadataInstances_original_name_by_source_id(self, source_id):
        """
        通过connection id获取模型相关信息
        :param source_id:
        :return:{{'meta': [], 'count': 0}}
        """
        limit = 20
        skip = 0
        meta_info = {'meta': [], 'count': 0, 'meta_id': []}
        while True:
            _filter = {
                "order": "last_updated DESC",
                "limit": limit,
                "fields": {
                    'name': True,
                    'owner': True,
                    'meta_type': True,
                    'description': True,
                    'db': True,
                    'stats': True,
                    'classifications': True,
                    'last_user_name': True,
                    'last_updated': True,
                    'create_time': True,
                    'collection': True,
                    'source': True,
                    'databaseId': True,
                    'user_id': True,
                    'id': True,
                    "original_name": True,
                    "qualified_name": True,
                },
                "skip": skip,
                "where": {
                    "is_deleted": False,
                    'source.id': source_id,
                    "meta_type": {
                        "in": [
                            "table",
                            "view",
                            "collection"
                        ]
                    }
                }
            }
            response = self._get_metadataInstances_by_filter(_filter)
            items = response.get('items')
            if items:
                for one_meta in items:
                    meta_info['meta'].append(one_meta.get('original_name'))
                    meta_info['meta_id'].append(one_meta.get('id'))
            else:
                break
            skip += limit
        response = self._get_metadataInstances_by_filter(_filter)
        meta_info['count'] = response.get('total')
        return meta_info

    # 通过数据源
    # 通过数据源id和表名获取模型id
    def get_metadataInstances_id_by_source_id_and_table_name(self, source_id, table_name):
        """
        通过connection id获取模型相关信息
        :param source_id:
        :return:{{'meta': [], 'count': 0}}
        """
        limit = 20
        skip = 0
        result = None
        while True:
            _filter = {
                "order": "last_updated DESC",
                "limit": limit,
                "fields": {
                    'id': True,
                    # "original_name": True,
                },
                "skip": skip,
                "where": {
                    "is_deleted": False,
                    'source.id': source_id,
                    'original_name': table_name,
                    "meta_type": {
                        "in": [
                            "table",
                            "view",
                            "collection"
                        ]
                    }
                }
            }
            response = self._get_metadataInstances_by_filter(_filter)
            items = response.get('items')
            if items:
                result = items[0]
            else:
                break
            skip += limit
        return result

    # 通过meta_id删除数据源
    def delete_metadataInstances_by_id(self, meta_id):
        self._delete_metadataInstances_by_id(meta_id)

    # 通过meta_id 获取表的模型
    def get_metadataInstances_find_table_by_id(self, meta_id):
        response = self._get_metadataInstances_find_table_by_id(meta_id)
        return response

    # 通过meta_id 修改meta
    def patch_metadataInstances_by_id(self, meta_id, payload):
        self._patch_metadataInstances_by_id(meta_id, payload)

    # 通过table查询
    def get_metadataInstances_search_table(self, keyword):
        response = self._get_metadataInstances_search('table', keyword)
        return response

    # 通过column查询
    def get_metadataInstances_search_column(self, keyword, last_id):
        response = self._get_metadataInstances_search('column', keyword, last_id)
        return response

    # 通过connection_id获取下面的表
    def get_metadataInstances_table_by_connection_id(self, connection_id):
        response = self._get_metadataInstances_table_by_connection_id(connection_id)
        return response
